#include "common.hpp"
#include "inet_addr.hpp"
int main(int argc,char* argv[])
{
    if(argc<3)
    {
        std::cerr<<"Please enter# ip port"<<std::endl;
        exit(INPUT_ERR);
    }
    std::string ip=argv[1];
    uint16_t port=std::stoi(argv[2]);
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        LOG(log_level::FATAL)<<"get socket err";
        exit(GET_SOCKET_ERR);
    }
    LOG(log_level::INFO)<<"get socket success: "<<sockfd;
    InetAddr local(ip,port);

    int connect_ret=connect(sockfd,TO_SOCKADDR(local.Addr()),sizeof(local.Addr()));
    if(connect_ret!=0)
    {
        LOG(log_level::FATAL)<<"connect err";
        exit(CONNECT_ERR);
    }
    LOG(log_level::INFO)<<"connect success";
    while(true)
    {
        std::string line;
        std::cout<<"Please enter# "<<std::endl;
        std::getline(std::cin,line);
        write(sockfd,line.c_str(),sizeof(line));
        char buffer[1024];
        int read_num=read(sockfd,buffer,sizeof(buffer)-1);
        if(read_num>0)
        {
            buffer[read_num]=0;
            std::cout<<"server said# "<<buffer;
        }
    }
    return 0;

}